package com.mbyte.easy.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.Group;
import com.mbyte.easy.admin.service.IGroupService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.util.PageInfo;
import com.mbyte.easy.util.Utility;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/admin/group")
public class GroupController extends BaseController  {

    private String prefix = "admin/group/";

    @Autowired
    private IGroupService groupService;

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param group
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, Group group) {
        Page<Group> page = new Page<Group>(pageNo, pageSize);
        QueryWrapper<Group> queryWrapper = new QueryWrapper<Group>();
        if(!ObjectUtils.isEmpty(group.getGroupId())) {
            queryWrapper = queryWrapper.like("group_id",group.getGroupId());
         }
        if(!ObjectUtils.isEmpty(group.getUserId())) {
            queryWrapper = queryWrapper.like("user_id",group.getUserId());
         }
        IPage<Group> pageInfo = groupService.page(page, queryWrapper);
        model.addAttribute("searchInfo", group);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }
    /**
     *@author 孙祺至
     *@date 2019/12/19 16:35
     */
//
//    @RequestMapping("SectionLeaderUnReview")
//    public String SectionLeaderUnReview(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, Group group) {
//        Page<Group> page = new Page<Group>(pageNo, pageSize);
//        QueryWrapper<Group> queryWrapper = new QueryWrapper<Group>();
//        if(!ObjectUtils.isEmpty(group.getGroupId())) {
//            queryWrapper = queryWrapper.like("group_id",group.getGroupId());
//         }
//        if(!ObjectUtils.isEmpty(group.getUserId())) {
//            queryWrapper = queryWrapper.like("user_id",group.getUserId());
//         }
//        String username= Utility.getCurrentUsername();
//        SysUser sysUser=groupService.getUserByUserName(username);
//        queryWrapper.lambda().eq(Group::getUserId,sysUser.getId());
//        IPage<Group> pageInfo = groupService.page(page, queryWrapper);
//        model.addAttribute("searchInfo", group);
//        model.addAttribute("pageInfo", new PageInfo(pageInfo));
//        return prefix+"list";
//    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }
    /**
    * 添加
    * @param group
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(Group group){
        return toAjax(groupService.save(group));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("group",groupService.getById(id));
        return prefix+"edit";
    }
    /**
    * 添加
    * @param group
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Group group){
        return toAjax(groupService.updateById(group));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(groupService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(groupService.removeByIds(ids));
    }

}

